package com.yanze.schoolinformation.view.my.person;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import com.yanze.schoolinformation.R;
import com.yanze.schoolinformation.app.AppConfig;
import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.base.BaseActivity;
import com.yanze.schoolinformation.databinding.ActivityPersonBinding;
import com.yanze.schoolinformation.helper.ImageHelper;
import com.yanze.schoolinformation.model.bean.resp.RespCurr;
import com.yanze.schoolinformation.model.bean.resp.RespUser;
import com.yanze.schoolinformation.utils.DialogUtils;
import com.yanze.schoolinformation.utils.FilePathManager;
import com.yanze.schoolinformation.utils.PerfectClickListener;
import com.yanze.schoolinformation.utils.ToastUtils;
import com.yanze.schoolinformation.utils.ToolUtils;
import com.yanze.schoolinformation.viewModule.person.PersonNavigator;
import com.yanze.schoolinformation.viewModule.person.PersonViewModel;

import java.io.File;
import java.io.IOException;

public class PersonActivity extends BaseActivity<ActivityPersonBinding> implements PersonNavigator {

    private BottomSheetDialog bottomDialog;//弹出框
    private PersonViewModel viewModel;
    private RespUser.UserInfo userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person);
        setTitle("个人资料");
        userInfo = (RespUser.UserInfo) getIntent().getSerializableExtra("userbean");
        initView();
        showContentView();
    }

    private void initView() {
        MyOnClick onclick = new MyOnClick();
        ImageHelper.displayCircle(bindingView.ivHeader, C.url.API_IMAGE + userInfo.getUsericon());
        bindingView.rlHeader.setOnClickListener(onclick);
        bindingView.rlNickname.setOnClickListener(onclick);
        bindingView.rlUserqq.setOnClickListener(onclick);
        bindingView.rlUserwechat.setOnClickListener(onclick);
        viewModel = new PersonViewModel(this);
        viewModel.setmNavigator(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        userInfo = AppConfig.getUserInfo(this);

        bindingView.tvNickname.setText(userInfo.getNickname());
        bindingView.tvUserqq.setText(userInfo.getUserqq());
        bindingView.tvUserwechat.setText(userInfo.getUserwechat());
    }

    @Override
    public void uploadSuccess(RespUser entity) {
        DialogUtils.dismissDialog();
        ToastUtils.show(this, entity.getMsg());
    }

    @Override
    public void uploadFail(String msg) {
        DialogUtils.dismissDialog();
        ToastUtils.show(this, msg);
    }

    @Override
    public void showImage(String imgurl) {
        ImageHelper.displayCircle(bindingView.ivHeader, C.url.API_IMAGE + imgurl);
        //同步本地的User信息
        RespUser.UserInfo userInfo = AppConfig.getUserInfo(this);
        if (userInfo != null) {
            userInfo.setUsericon(imgurl);
            AppConfig.saveUserInfo(this,userInfo);
        }
    }

    private class MyOnClick extends PerfectClickListener {

        @Override
        protected void onNoDoubleClick(View v) {
            switch (v.getId()) {
                case R.id.rl_header:
                    if (bottomDialog == null) {
                        View diaView = LayoutInflater.from(PersonActivity.this).inflate(R.layout.dialog_header, null);
                        Button local = diaView.findViewById(R.id.head_fromlocal);
                        local.setOnClickListener(MyOnClick.this);
                        Button camera = diaView.findViewById(R.id.head_fromcamera);
                        camera.setOnClickListener(MyOnClick.this);
                        Button cancal = diaView.findViewById(R.id.head_cancel);
                        cancal.setOnClickListener(MyOnClick.this);
                        bottomDialog = new BottomSheetDialog(PersonActivity.this);
                        bottomDialog.setContentView(diaView);
                        bottomDialog.getWindow().findViewById(R.id.design_bottom_sheet)
                                .setBackgroundResource(android.R.color.transparent);
                    }
                    bottomDialog.show();
                    break;
                case R.id.rl_nickname:
                    ModifyActivity.start(PersonActivity.this,1);
                    break;
                case R.id.rl_userqq:
                    ModifyActivity.start(PersonActivity.this,2);
                    break;
                case R.id.rl_userwechat:
                    ModifyActivity.start(PersonActivity.this,3);
                    break;
                case R.id.head_fromlocal://本地相册获取图片
                    if (bottomDialog != null) {
                        bottomDialog.dismiss();
                    }
                    viewModel.checkPermissionForNormal(Manifest.permission.WRITE_EXTERNAL_STORAGE);//读取图片权限
                    break;
                case R.id.head_fromcamera://拍照
                    if (bottomDialog != null) {
                        bottomDialog.dismiss();
                    }
                    viewModel.checkPermissionForNormal(Manifest.permission.CAMERA);//拍照权限
                    break;
                case R.id.head_cancel:
                    if (bottomDialog != null) {
                        bottomDialog.dismiss();
                    }
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case PersonViewModel.REQUEST_LOCAL://获取本地图片
                if (resultCode == RESULT_OK) {
                    if (data != null) {
                        Uri uri = data.getData();
                        String filePath = ToolUtils.getRealPathFromUriAboveApi19(this, uri);
                        if (filePath == null || filePath.equals("")) {
                            ToastUtils.show("路径获取失败，请重新选择");
                            return;
                        }
                        CropActivity.start(this, filePath);
                    }
                }
                break;
            case PersonViewModel.REQUEST_CAMERA://拍照
                if (resultCode == RESULT_OK) {
                    File cameraFile = FilePathManager.getCameraCache(this);
                    if (cameraFile != null) {
                        String filePath = cameraFile.getAbsolutePath();
                        CropActivity.start(this, filePath);
                    }
                }
                break;
            case PersonViewModel.REQUEST_CROP:
                if (resultCode == PersonViewModel.RESULT_CROP) {
                    DialogUtils.showDialog(this, "请稍后...");
                    viewModel.UpLoadHeader();
                }
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        viewModel.requestPermissionResult(requestCode, permissions, grantResults);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewModel.onDestory();
    }

    /**
     * 入口
     */
    public static void start(Context context, RespUser.UserInfo user) {
        Intent intent = new Intent(context, PersonActivity.class);
        intent.putExtra("userbean", user);
        context.startActivity(intent);
    }
}
